package	com.sdry.web.controller.tdd.refunds;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdry.model.jyy.SimpleJyy;
import com.sdry.model.llm.LlmCancellingStockReason;
import com.sdry.model.llm.LlmCancellingStocks;
import com.sdry.model.llm.LlmCancellingStocksDetail;
import com.sdry.model.lz.Customer;
import com.sdry.model.lz.OutStockOrderDetailPDAEntity;
import com.sdry.model.lz.Warehouse;
import com.sdry.service.jyy.SimpleServise;
import com.sdry.service.llm.CancellingStockDetailService;
import com.sdry.service.llm.CancellingStockReasonService;
import com.sdry.service.llm.CancellingStockService;
import com.sdry.service.lz.StockOutOrderService;
import com.sdry.service.tdd.out.OutStockOrderDetail2Service;
import com.sdry.service.tdd.refunds.CancellingStocksDetailPiLiangService;
import com.sdry.utils.Page;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;
/**
 *
 *@ClassName: CancellingStocksDetailPiLiangController
 *@Description: 退货单——批次退货
 *@Author tdd
 *@Date 2019-10-17 18:27:11
 *@version 1.0
*/
@Controller
@RequestMapping("/CancellingStocksDetailPiLiang")
public class CancellingStocksDetailPiLiangController{
	@Resource CancellingStocksDetailPiLiangService CancellingStocksDetailPiLiangService;
	@Resource CancellingStockService cancellingStockService;
	@Resource CancellingStockReasonService cancellingStockReasonService;
	@Resource CancellingStockDetailService cancellingStockDetailService;
	@Resource OutStockOrderDetail2Service outStockOrderDetail2Service;
	@Resource SimpleServise simpleServise;
	@Resource StockOutOrderService stockOutOrderService;
	/**
	 * 进入退库单管理
	 * @return
	 */
	@RequestMapping(value="/toCancellingStocksManagerPiLiang")
	public ModelAndView toCancellingStocksManagerPiLiang(){
		ModelAndView mv = new ModelAndView();
		List<Customer> customerList = stockOutOrderService.queryAllCustomerByType();
		//List<Customer> customerList = cancellingStockService.getAllCustomer();
		mv.addObject("customerList", customerList);
		List<LlmCancellingStockReason> reasonList = cancellingStockReasonService.getAllCancellingStockReason();
		mv.addObject("reasonList", reasonList);
		List<Warehouse> allWarehouse = cancellingStockService.getAllWarehouse();
		mv.addObject("warehouseList", allWarehouse);
		mv.setViewName("/llm/CancellingStocksManager_piLiang");
		return mv;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<LlmCancellingStocksDetail> queryAllByMution(LlmCancellingStocksDetail param) {
		List<LlmCancellingStocksDetail> list = CancellingStocksDetailPiLiangService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(LlmCancellingStocksDetail param,HttpServletResponse response, HttpSession session) throws Exception{
		/*
		 * 条件分页查询内容
		 */
		List<LlmCancellingStocksDetail> list = CancellingStocksDetailPiLiangService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = CancellingStocksDetailPiLiangService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		//创建result
		JSONObject jsonObject = new JSONObject();
		//添加入jsonObject
		jsonObject.put("code", 0);
		jsonObject.put("rows", list);
		jsonObject.put("total", total);
		ResponseUtil.write(response, jsonObject);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/insertPiliang")
	public void insertPiliang(String ids, String cancellingNumber, String source, String warehouseId, String customerId,String mbatchs,String locations,String isUnpackings, HttpServletResponse response) throws Exception{
		String[] split = ids.split(",");
		String[] split1 = mbatchs.split(",");
		String[] split2 = locations.split(",");
		String[] split3 = isUnpackings.split(",");
		int isSuccess = 0;
		LlmCancellingStocksDetail cancellingStockDetail = new LlmCancellingStocksDetail();
		cancellingStockDetail.setCancellingNumber(cancellingNumber);
		cancellingStockDetail.setStatus(0);
		for (int i = 0; i < split.length; i++) {
			cancellingStockDetail.setMaterialId(Long.parseLong(split[i]));
			cancellingStockDetail.setMbatch(split1[i]);
			cancellingStockDetail.setLocation(split2[i]);
			cancellingStockDetail.setIsUnpacking(StringUtil.strNum(split3[i]));
			//根据物料ID查库存
			/*Integer good = cancellingStockDetailMapper.getGoodQuantityInInventoryInfo(Long.parseLong(id));
			Integer bad = cancellingStockDetailMapper.getBadQuantityInRejectsWarehouse(Long.parseLong(id));
			if(good != null){
				cancellingStockDetail.setGoodQuantity(good);
			}else{
				cancellingStockDetail.setGoodQuantity(0);
			}
			if(bad != null){
				cancellingStockDetail.setBadQuantity(bad);
			}else{
				cancellingStockDetail.setBadQuantity(0);
			}
			cancellingStockDetail.setSumQuantity(cancellingStockDetail.getGoodQuantity() + cancellingStockDetail.getBadQuantity());*/
			cancellingStockDetail.setMaterialQuantity(0);
			Long id = CancellingStocksDetailPiLiangService.insert(cancellingStockDetail);
			if(null != id && id > 0){
				isSuccess++;
			}
		}
		ResponseUtil.write(response,isSuccess);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(LlmCancellingStocksDetail param,HttpServletResponse response) throws Exception{
		Integer count = CancellingStocksDetailPiLiangService.update(param);
		ResponseUtil.write(response,count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response) throws Exception{
		ids=ids.replace("'",""); 
		Integer count = CancellingStocksDetailPiLiangService.delete(ids);
		ResponseUtil.write(response,count);
	}
	/******************************************************APP*************************************************/
	/**
	 * 查询所有退库单
	 * @Title:appGetCancellingStock  
	 * @param response
	 */
	@RequestMapping("/appGetCancellingStock")
	public void appGetCancellingStock(HttpServletResponse response){
		//根据查询条件lc.status = 0 AND lc.flag != 0  AND lc.type = 0 查询
		Map<String, Object> map = new HashMap<>();
		map.put("page", 1);
		map.put("cancellingReason", "");
		map.put("keyword03", " AND flag != 0  AND type = 1 ");
		map.put("status","0");
		int count = cancellingStockService.getCountConcellingStock(map);
		map.put("limit", count);
		List<LlmCancellingStocks> list = cancellingStockService.listPageConcellingStock(map);
		//List<LlmCancellingStocks> list = cancellingStockService.getAllCancellingStock();
		JSONObject jsonObject = new JSONObject();
		if(list.size() > 0){
			jsonObject.put("status", "1");
			jsonObject.put("data", list);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/selectLlmCancellingStocksDetailByCancellingNumber")
	public void selectLlmCancellingStocksDetailByCancellingNumber(String cancellingNumber,HttpServletResponse response) {
		LlmCancellingStocksDetail param = new LlmCancellingStocksDetail();
		param.setCancellingNumber(cancellingNumber);
		List<LlmCancellingStocksDetail> list = CancellingStocksDetailPiLiangService.queryAllByMution(param);
		JSONObject jsonObject = new JSONObject();
		if(list.size() > 0){
			jsonObject.put("status", "1");
			jsonObject.put("data", list);
		}else{
			jsonObject.put("status", "0");
			jsonObject.put("message", "查询数据为空");
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * PDA根据批量出库单id查询详情接口
	 * @param json app提交的出库单详细实体
	 * @param response
	 */
	@RequestMapping("/piLiangSubmission")
	public void piLiangSubmission(HttpServletResponse response, String json){
		//json转list集合
		JSONObject jsonObject = new JSONObject();
		Gson gson = new Gson();
		List<LlmCancellingStocksDetail> LlmCancellingStocksDetailList = gson.fromJson(json, new TypeToken<List<LlmCancellingStocksDetail>>() {}.getType());
		if(LlmCancellingStocksDetailList.size() > 0) {
			//库存是否充足标识
			int flg = 0;
			for(LlmCancellingStocksDetail detail : LlmCancellingStocksDetailList){
				SimpleJyy simpleJyy = new SimpleJyy();
				simpleJyy.setMid(detail.getMateriel().getId());
				simpleJyy.setmBatch(detail.getMbatch());
				simpleJyy.setLocation(detail.getLocation());
				simpleJyy.setIsUnpacking(detail.getIsUnpacking()+"");
				SimpleJyy selectByAll = simpleServise.selectByAll(simpleJyy);
				if(null == selectByAll || selectByAll.getmNum() < detail.getActualQuantity()){
					flg = 1;
					break;
				}
			}
			if(flg == 0){
				for(LlmCancellingStocksDetail detail : LlmCancellingStocksDetailList){
					cancellingStockDetailService.updateCancellingStockDetailActNum(detail.getId() + "",
							detail.getActualQuantity() + "", detail.getSingle_num() + "",
							detail.getBig_num() + "", detail.getSmall_num() + "");
					//1.根据APP传递过来的数据修改数量
					outStockOrderDetail2Service.inventoryReductionOnlyCount(detail.getMateriel().getId(),detail.getMbatch(),detail.getLocation(),detail.getIsUnpacking(), detail.getActualQuantity());
				}
				Map<String, Object> map = new HashMap<>();
				map.put("cancellingNumber", LlmCancellingStocksDetailList.get(0).getCancellingNumber());
				map.put("fieldName", "status");
				map.put("fieldValue", "1");
				cancellingStockService.updateConcellingStock2(map);
				jsonObject.put("status", "1");
				jsonObject.put("data", "退货成功");
			}else{
				jsonObject.put("status", "0");
				jsonObject.put("message", "库存不足");
			}
		} else {
			jsonObject.put("status", "0");
			jsonObject.put("message", "提交数据为空");
		}
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
